package chapter17LockSupport;

import java.time.LocalDateTime;
import java.util.concurrent.locks.LockSupport;

/**
 * Created on 2019/6/24
 * @author LinGer
 */
public class LockSupportTest extends Thread {

    private static Thread mainThread;

    public LockSupportTest(String name){
        super(name);
    }

    @Override
    public void run() {
        try {
            System.out.println(this.getName() + " sleep, " + LocalDateTime.now());
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(this.getName() + " wakeup, " + LocalDateTime.now());
        //唤醒主线程
        LockSupport.unpark(mainThread);
    }

    public static void main(String[] args) {

        LockSupportTest t1 = new LockSupportTest("t1");
        //获取主线程
        mainThread = Thread.currentThread();

        System.out.println(Thread.currentThread().getName() + " start t1, " + LocalDateTime.now());
        t1.start();

        //主线程阻塞
        System.out.println(Thread.currentThread().getName() + " block, " + LocalDateTime.now());
        LockSupport.park(mainThread);

        System.out.println(Thread.currentThread().getName() + " continue, " + LocalDateTime.now());
    }
}
